home *** CD-ROM | disk | FTP | other *** search
- /*
- File: Planet.c
-
- Contains: Planetary orbit display templates
-
- Written by: Harry Chesley
-
- Copyright: © 1994 by Apple Computer, Inc.
-
- Change History (most recent first):
-
- 5/25/94 hrc created
-
- To Do:
- */
-
- #include <SANE.h>
- #include <String.h>
- #include <Packages.h>
- #include <Script.h>
- #include <OSUtils.h>
- #include <Math.h>
- #include <TextUtils.h>
- #include "OCETemplates.h"
- #include "Planet.h"
-
- OSErr instanceInit(DETCallBlockPtr);
- OSErr convertToRString(DETCallBlockPtr);
- OSErr convertFromRString(DETCallBlockPtr);
- OSErr propertyDirtied(DETCallBlockPtr);
-
- extended getExtendedProperty(DETCallBlockPtr, short);
- LongDateTime getTimeProperty(DETCallBlockPtr, short);
-
- pascal OSErr Planet(DETCallBlockPtr callBlockPtr)
- {
- if (callBlockPtr->protoCall.target.selector == kDETSelf)
- switch (callBlockPtr->protoCall.reqFunction)
- {
- case kDETcmdInstanceInit: return instanceInit(callBlockPtr);
- case kDETcmdConvertToRString: return convertToRString(callBlockPtr);
- case kDETcmdConvertFromRString: return convertFromRString(callBlockPtr);
- case kDETcmdPropertyDirtied: return propertyDirtied(callBlockPtr);
- }
-
- else if (callBlockPtr->protoCall.reqFunction == kDETcmdInit)
- {
- callBlockPtr->init.newCallFors = kDETCallForViewChanges;
- return noErr;
- }
-
- return kDETDidNotHandle;
- }
-
- OSErr instanceInit(DETCallBlockPtr callBlockPtr)
- {
- DETSetPropertyTypeBlock spt;
- DETSetPropertyBinaryBlock spb;
- unsigned long l;
- LongDateCvt ldt;
-
- // Set the time property type
- spt.reqFunction = kDETcmdSetPropertyType;
- spt.target = callBlockPtr->protoCall.target;
- spt.property = kTimeProperty;
- spt.newType = kTimePropertyType;
- CallBackDET(callBlockPtr, (DETCallBackBlock*) &spt);
-
- // Set the time property to the current time
- GetDateTime(&l);
- ldt.hl.lHigh = 0;
- ldt.hl.lLow = l;
- spb.reqFunction = kDETcmdSetPropertyBinary;
- spb.target = callBlockPtr->protoCall.target;
- spb.property = kTimeProperty;
- spb.newValue = (Ptr) &ldt;
- spb.newValueSize = sizeof(ldt);
- if (CallBackDET(callBlockPtr, (DETCallBackBlock*) &spb) == noErr)
- {
- // Dirty it
- DETDirtyPropertyBlock dp;
-
- dp.reqFunction = kDETcmdDirtyProperty;
- dp.target = callBlockPtr->protoCall.target;
- dp.property = kTimeProperty;
- CallBackDET(callBlockPtr, (DETCallBackBlock*) &dp);
- }
-
- return noErr;
- }
-
- OSErr convertToRString(DETCallBlockPtr callBlockPtr)
- {
- DETConvertToRStringBlock* ctrs;
- DETGetPropertyTypeBlock gpt;
-
- ctrs = &(callBlockPtr->convertToRString);
-
- // Get the type of the property being converted
- gpt.reqFunction = kDETcmdGetPropertyType;
- gpt.target = ctrs->target;
- gpt.property = ctrs->property;
- if (CallBackDET(callBlockPtr, (DETCallBackBlock*) &gpt) == noErr)
- {
- char s[256];
- RStringHandle h;
-
- // Convert time properties
- if (gpt.propertyType == kTimePropertyType)
- {
- LongDateTime ldt;
- char tStr[256];
-
- // Get the current value
- ldt = getTimeProperty(callBlockPtr, ctrs->property);
-
- // Convert it to a string
- iuldatestring(&ldt, shortDate, s, nil);
- tStr[0] = ' ';
- tStr[1] = 0;
- strcat(s, tStr);
- iultimestring(&ldt, true, tStr, nil);
- strcat(s, tStr);
- }
-
- // Convert floating point extended properties
- else if (gpt.propertyType == kExtendedPropertyType)
- {
- extended n;
- decform df;
- decimal d;
-
- // Get the current value
- n = getExtendedProperty(callBlockPtr, ctrs->property);
-
- // Convert it to a string
- df.style = FLOATDECIMAL;
- df.digits = 9;
- num2dec(&df, n, &d);
- dec2str(&df, &d, &s);
- }
-
- // If we don't know the type, don't convert it
- else return kDETDidNotHandle;
-
- // Return the string as an RString handle
- h = (RStringHandle) NewHandle(strlen(s) + sizeof(ProtoRString));
- if (h)
- {
- HLock((Handle) h);
- OCECToRString(s, smRoman, *h, strlen(s));
- HUnlock((Handle) h);
- ctrs->theValue = h;
-
- return noErr;
- }
- else return MemError();
- }
-
- return kDETDidNotHandle;
- }
-
- OSErr convertFromRString(DETCallBlockPtr callBlockPtr)
- {
- OSErr retVal;
- DETConvertFromRStringBlock* cfrs;
- DETGetPropertyTypeBlock gpt;
-
- retVal = kDETDidNotHandle;
- cfrs = &(callBlockPtr->convertFromRString);
-
- // Get the property type to convert
- gpt.reqFunction = kDETcmdGetPropertyType;
- gpt.target = cfrs->target;
- gpt.property = cfrs->property;
- if (CallBackDET(callBlockPtr, (DETCallBackBlock*) &gpt) == noErr)
- {
- char* str;
-
- // Convert the RString into a string
- str = ((char*) &cfrs->theValue->dataLength) + 1;
- p2cstr(str);
-
- // Convert time properties
- if (gpt.propertyType == kTimePropertyType)
- {
- DateCacheRecord dc;
-
- // Init the date cache
- if (InitDateCache(&dc) == noErr)
- {
- long lengthUsed;
- LongDateRec ldr;
- LongDateTime ldt;
- long strLength;
- DETSetPropertyBinaryBlock spb;
-
- // Convert the string to a time
- strLength = strlen(str);
- String2Date(str, strLength, &dc, &lengthUsed, &ldr);
- if ((strLength - lengthUsed) <= 0)
- {
- ldr.ld.hour = 0;
- ldr.ld.minute = 0;
- ldr.ld.second = 0;
- }
- else String2Time(str + lengthUsed, strLength - lengthUsed, &dc, &lengthUsed, &ldr);
- LongDate2Secs(&ldr, &ldt);
-
- // Set the property value
- spb.reqFunction = kDETcmdSetPropertyBinary;
- spb.target = cfrs->target;
- spb.property = cfrs->property;
- spb.newValue = (Ptr) &ldt;
- spb.newValueSize = sizeof(ldt);
- if (CallBackDET(callBlockPtr, (DETCallBackBlock*) &spb) == noErr)
- {
- // Mark it as changed
- DETSetPropertyChangedBlock spc;
- spc.reqFunction = kDETcmdSetPropertyChanged;
- spc.target = cfrs->target;
- spc.property = cfrs->property;
- spc.propertyChanged = true;
- CallBackDET(callBlockPtr, (DETCallBackBlock*) &spc);
- }
- retVal = noErr;
- }
- }
-
- // Convert floating point extended properties
- else if (gpt.propertyType == kExtendedPropertyType)
- {
- DETSetPropertyBinaryBlock spb;
- extended n;
-
- // Convert the string to an extended
- n = 0;
- if (cfrs->theValue->dataLength < 256)
- {
- short ix;
- decimal d;
- short vp;
-
- ix = 0;
- str2dec(str, &ix, &d, &vp);
- if (vp) n = dec2num(&d);
- }
-
- // Set the value of the property
- spb.reqFunction = kDETcmdSetPropertyBinary;
- spb.target = cfrs->target;
- spb.property = cfrs->property;
- spb.newValue = (Ptr) &n;
- spb.newValueSize = sizeof(n);
- if (CallBackDET(callBlockPtr, (DETCallBackBlock*) &spb) == noErr)
- {
- // Mark it as changed
- DETSetPropertyChangedBlock spc;
- spc.reqFunction = kDETcmdSetPropertyChanged;
- spc.target = cfrs->target;
- spc.property = cfrs->property;
- spc.propertyChanged = true;
- CallBackDET(callBlockPtr, (DETCallBackBlock*) &spc);
- }
- retVal = noErr;
- }
-
- // Return the original to it's normal state
- c2pstr(str);
- }
-
- return retVal;
- }
-
- // AU in meters
- #define kAU 149600000.0
-
- // Sin with degrees
- extended degsin(extended d)
- {
- return sin(pi()*d/180.0);
- }
-
- // Cos with degrees
- extended degcos(extended d)
- {
- return cos(pi()*d/180.0);
- }
-
- // Returns days (including fractions) since 1990
- extended daysSince1990(LongDateTime t)
- {
- LongDateRec ldr;
- LongDateTime t1990;
- extended et, et1990;
-
- et = t;
-
- ldr.ld.era = 0;
- ldr.ld.year = 1989;
- ldr.ld.month = 12;
- ldr.ld.day = 31;
- ldr.ld.hour = 0;
- ldr.ld.minute = 0;
- ldr.ld.second = 0;
- ldr.ld.pm = 0;
- LongDate2Secs(&ldr, &t1990);
- et1990 = t1990;
-
- return et / (24.0*60.0*60.0) - et1990 / (24.0*60.0*60.0);
- }
-
- OSErr propertyDirtied(DETCallBlockPtr callBlockPtr)
- {
- DETPropertyDirtiedBlock* pd;
-
- pd = (DETPropertyDirtiedBlock*) &callBlockPtr->propertyDirtied;
- switch (pd->property)
- {
- // Only recalculate on selected properties
- case kTimeProperty:
- case kTpProperty:
- case kEpsilonProperty:
- case kOmegaBarProperty:
- case keProperty:
- case kaProperty:
- {
- DETSetPropertyTypeBlock spt;
- DETSetPropertyBinaryBlock spb;
- extended d, tp, epsilon, omegaBar, e, a;
- extended n, m, l, v, r, x, y;
-
- // Get the input parameters
- d = daysSince1990(getTimeProperty(callBlockPtr, kTimeProperty));
- tp = getExtendedProperty(callBlockPtr, kTpProperty);
- epsilon = getExtendedProperty(callBlockPtr, kEpsilonProperty);
- omegaBar = getExtendedProperty(callBlockPtr, kOmegaBarProperty);
- e = getExtendedProperty(callBlockPtr, keProperty);
- a = getExtendedProperty(callBlockPtr, kaProperty);
-
- // If the parameters are zero, return zero
- if (tp == 0.0)
- {
- x = 0.0;
- y = 0.0;
- }
- // Otherwise, calculate the current position
- else
- {
- n = fmod((360.0/365.242191)*(d/tp), 360.0);
- m = n+epsilon-omegaBar;
- l = fmod(n+(360.0/pi())*e*degsin(m)+epsilon, 360.0);
- v = l-omegaBar;
- r = kAU*(a*(1.0-e*e))/(1.0+e*degcos(v));
- x = degcos(l)*r;
- y = degsin(l)*r;
- }
-
- // Prepare to set the type and value of the x and y properties
- spt.reqFunction = kDETcmdSetPropertyType;
- spt.target = pd->target;
-
- spb.reqFunction = kDETcmdSetPropertyBinary;
- spb.target = pd->target;
-
- // Set x's type
- spt.property = kXProperty;
- spt.newType = kExtendedPropertyType;
- if (CallBackDET(callBlockPtr, (DETCallBackBlock*) &spt) == noErr)
- {
- // Set x's value
- spb.property = kXProperty;
- spb.newValue = (Ptr) &x;
- spb.newValueSize = sizeof(x);
- CallBackDET(callBlockPtr, (DETCallBackBlock*) &spb);
- }
-
- // Set y's type
- spt.property = kYProperty;
- spt.newType = kExtendedPropertyType;
- if (CallBackDET(callBlockPtr, (DETCallBackBlock*) &spt) == noErr)
- {
- // Set y's value
- spb.property = kYProperty;
- spb.newValue = (Ptr) &y;
- spb.newValueSize = sizeof(y);
- CallBackDET(callBlockPtr, (DETCallBackBlock*) &spb);
- }
-
- return noErr;
- }
- }
-
- return kDETDidNotHandle;
- }
-
- extended getExtendedProperty(DETCallBlockPtr callBlockPtr, short property)
- {
- DETGetPropertyBinaryBlock gpb;
- extended n;
-
- gpb.reqFunction = kDETcmdGetPropertyBinary;
- gpb.target = callBlockPtr->protoCall.target;
- gpb.property = property;
- if (CallBackDET(callBlockPtr, (DETCallBackBlock*) &gpb) != noErr) return 0.0;
-
- BlockMove(*gpb.propertyValue, (char*) &n, sizeof(n));
- DisposHandle(gpb.propertyValue);
-
- return n;
- }
-
- LongDateTime getTimeProperty(DETCallBlockPtr callBlockPtr, short property)
- {
- DETGetPropertyBinaryBlock gpb;
- LongDateTime ldt;
-
- gpb.reqFunction = kDETcmdGetPropertyBinary;
- gpb.target = callBlockPtr->protoCall.target;
- gpb.property = property;
- if (CallBackDET(callBlockPtr, (DETCallBackBlock*) &gpb) != noErr) return 0;
-
- BlockMove(*gpb.propertyValue, (char*) &ldt, sizeof(ldt));
- DisposHandle(gpb.propertyValue);
-
- return ldt;
- }
-